<template>
  <el-dialog title="编辑商品积分规则" :visible.sync="visible" width="700px" @closed="reset">
    <el-form ref="dataForm" class="form" :model="row" label-position="right" label-width="100px">
      <el-form-item label="商品类型:">
        <el-input v-model="row.gtName" disabled />
      </el-form-item>
      <el-form-item label="商品编码:">
        <el-input v-model="row.goodsCode" disabled />
      </el-form-item>
      <el-form-item label="商品名称:">
        <el-input v-model="row.goodsName" disabled />
      </el-form-item>
      <el-form-item label="单位:">
        <el-input v-model="row.unitName" disabled />
      </el-form-item>
      <el-form-item label="居民用户:" class="block" style="width: 50%;">
        <span v-if="row.gtCategory === 4">兑换所需积分 <el-input-number v-model="csmType1.requiredScore" :disabled="disabled1" :min="0" :max="99999999" :precision="0" :controls="false" /></span>
        <span v-else>购买赠送积分 <el-input-number v-model="csmType1.givenScore" :min="0" :max="99999999" :precision="0" :controls="false" /></span>
      </el-form-item>
      <el-form-item label="商业用户:" prop="orderCode" class="block">
        <span v-if="row.gtCategory === 4">兑换所需积分 <el-input-number v-model="csmType2.requiredScore" :disabled="disabled2" :min="0" :max="99999999" :precision="0" :controls="false" /></span>
        <span v-else>购买赠送积分 <el-input-number v-model="csmType2.givenScore" :min="0" :max="99999999" :precision="0" :controls="false" /></span>
      </el-form-item>
      <el-form-item label="工业用户:" prop="orderCode" class="block">
        <span v-if="row.gtCategory === 4">兑换所需积分 <el-input-number v-model="csmType4.requiredScore" :disabled="disabled4" :min="0" :max="99999999" :precision="0" :controls="false" /></span>
        <span v-else>购买赠送积分 <el-input-number v-model="csmType4.givenScore" :min="0" :max="99999999" :precision="0" :controls="false" /></span>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { saveScoreRules, queryScoreRules } from '@/api/index'

  export default {
    name: 'editintegra',
    data() {
      return {
        visible: false,
        row: {},
        ruleList: [],
        org: null,
        goodsType: null,
        csmType1: {
          requiredScore: undefined,
          givenScore: undefined
        },
        csmType2: {
          requiredScore: undefined,
          givenScore: undefined
        },
        csmType4: {
          requiredScore: undefined,
          givenScore: undefined
        },
        disabled1: false,
        disabled2: false,
        disabled4: false
      }
    },
    methods: {
      open(row, org, goodsType) {
        this.org = org
        this.goodsType = goodsType
        this.getDetailData(row)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (row) {
            this.row = Object.assign(this.row, row)
          }
        })
      },
      async getDetailData(row) {
        const res = await queryScoreRules({
          goodsId: row.goodsId,
          orgSNNo: this.org.orgSNNo
        })
        this.ruleList = JSON.parse(res.data.dataList)
        if (this.ruleList.length > 0) {
          this.ruleList.forEach(item => {
            if (item.csmType === 1) {
              if (item.givenScore > 0) this.csmType1.givenScore = item.givenScore >> 1
              if (item.requiredScore > 0) this.csmType1.requiredScore = item.requiredScore >> 1
            } else if (item.csmType === 2) {
              if (item.givenScore > 0) this.csmType2.givenScore = item.givenScore >> 1
              if (item.requiredScore > 0) this.csmType2.requiredScore = item.requiredScore >> 1
            } else if (item.csmType === 4) {
              if (item.givenScore > 0) this.csmType4.givenScore = item.givenScore >> 1
              if (item.requiredScore > 0) this.csmType4.requiredScore = item.requiredScore >> 1
            }
          })
        }
        if (row.exchangeFlag === 1) {
          this.disabled1 = true
          this.csmType1.requiredScore = undefined
          this.disabled2 = true
          this.csmType2.requiredScore = undefined
          this.disabled4 = true
          this.csmType4.requiredScore = undefined
        }
      },
      success() {
        const obj = {
          orgId: this.org.orgId,
          orgSNNo: this.org.orgSNNo,
          orgPNNo: this.org.orgPNNo,
          orgName: this.org.orgName,
          gtId: this.goodsType.gtId,
          goodsId: this.row.goodsId,
          goodsName: this.row.goodsName,
          scoreStatus: 2
        }
        const data = []
        let obj1 = {
          csmType: 1
        }
        if (this.row.gtCategory === 4) {
          if (this.row.exchangeFlag !== 1) {
            if (!this.csmType1.requiredScore) {
              obj1.requiredScore = parseInt(this.csmType1.requiredScore)
            } else {
              obj1.requiredScore = (parseInt(this.csmType1.requiredScore) << 1) | 1
            }
          }
        } else {
          if (!this.csmType1.givenScore) {
            obj1.givenScore = parseInt(this.csmType1.givenScore)
          } else {
            obj1.givenScore = (parseInt(this.csmType1.givenScore) << 1) | 1
          }
        }
        obj1 = Object.assign(obj1, obj)
        data.push(obj1)
        let obj2 = {
          csmType: 2
        }
        if (this.row.gtCategory === 4) {
          if (this.row.exchangeFlag !== 1) {
            if (!this.csmType2.requiredScore) {
              obj2.requiredScore = parseInt(this.csmType2.requiredScore)
            } else {
              obj2.requiredScore = (parseInt(this.csmType2.requiredScore) << 1) | 1
            }
          }
        } else {
          if (!this.csmType2.givenScore) {
            obj2.givenScore = parseInt(this.csmType2.givenScore)
          } else {
            obj2.givenScore = (parseInt(this.csmType2.givenScore) << 1) | 1
          }
        }
        obj2 = Object.assign(obj2, obj)
        data.push(obj2)
        let obj4 = {
          csmType: 4
        }
        if (this.row.gtCategory === 4) {
          if (this.row.exchangeFlag !== 1) {
            if (!this.csmType4.requiredScore) {
              obj4.requiredScore = parseInt(this.csmType4.requiredScore)
            } else {
              obj4.requiredScore = (parseInt(this.csmType4.requiredScore) << 1) | 1
            }
          }
        } else {
          if (!this.csmType4.givenScore) {
            obj4.givenScore = parseInt(this.csmType4.givenScore)
          } else {
            obj4.givenScore = (parseInt(this.csmType4.givenScore) << 1) | 1
          }
        }
        obj4 = Object.assign(obj4, obj)
        data.push(obj4)
        this.submit(data)
      },
      async submit(params) {
        await saveScoreRules({ data: params, exchange: this.row.gtCategory === 4 ? 1 : -1 })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.csmType1 = {
          requiredScore: undefined,
          givenScore: undefined
        }
        this.csmType2 = {
          requiredScore: undefined,
          givenScore: undefined
        }
        this.csmType4 = {
          requiredScore: undefined,
          givenScore: undefined
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
